package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.ChessArray;
import model.User;
import util.SocketUtil;
import util.StateCode;

public class ChessPanel extends JPanel implements Observer, ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel[] player = new JLabel[2];
	private String[] s1 = { "开始", "悔棋", "和棋", "认输", "回放" };
	private JButton[] jb = new JButton[s1.length];
	private JLabel[] chessbox = new JLabel[2];
	private GridPanel gridPanel;
	private ChessArray chessArray;

	public ChessPanel() {
		setBackground(new Color(0xc0c0c0));
		setLayout(new BorderLayout());
		setOpaque(false);
		chessArray = new ChessArray();
		gridPanel = new GridPanel(chessArray);
		add(gridPanel);
		chessArray.addObserver(this);
		JPanel bottom = new JPanel();
		bottom.setOpaque(false);
		for (int i = 0; i < s1.length; i++) {
			jb[i] = new JButton(s1[i]);
			bottom.add(jb[i]);
			jb[i].addActionListener(this);
		}
		add(bottom, BorderLayout.SOUTH);
		// disableButton();
		JPanel top = new JPanel();
		top.setOpaque(false);
		JLabel title = new JLabel("网络五子棋");
		title.setFont(new Font("华文彩云", Font.ITALIC, 24));
		title.setForeground(new Color(0x008B00));
		JLabel author = new JLabel("            叶哲铭");
		author.setFont(new Font("华文琥珀", Font.BOLD, 18));
		author.setForeground(new Color(0x00E5EE));
		top.add(title);
		top.add(author);
		add(top, BorderLayout.NORTH);
		String[] s2 = { "player1", "player2" };
		JPanel[] playerpn = new JPanel[2];
		for (int i = 0; i < s2.length; i++) {
			playerpn[i] = new JPanel();
			playerpn[i].setOpaque(false);
			playerpn[i].setLayout(new GridBagLayout());
			GridBagConstraints gbc = new GridBagConstraints();
			JLabel image = new JLabel();
			image.setIcon(new ImageIcon(getClass().getResource("../images/p" + i + ".jpg")));
			player[i] = new JLabel(s2[i]);
			gbc.gridy = 0;
			gbc.insets = new Insets(10, 20, 10, 20);
			gbc.gridy = 1;
			playerpn[i].add(image, gbc);
			gbc.gridy = 3;
			chessbox[i] = new JLabel(s2[i]);
			chessbox[i].setPreferredSize(new Dimension(50, 37));
			playerpn[i].add(chessbox[i], gbc);
		}
		add(playerpn[0], BorderLayout.WEST);
		add(playerpn[1], BorderLayout.EAST);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Image backimg = new ImageIcon(getClass().getResource("../images/background.jpg")).getImage();
		g.drawImage(backimg, 0, 0, getWidth(), getHeight(), null);
		// System.out.println(getWidth());//754
		// System.out.println(getHeight());//562
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof User) {
			User user = (User) arg;
			if (o.equals(user))
				player[1].setText(user.getName());
			else
				player[0].setText(user.getName());
		} else if (arg instanceof Integer) {
			int code = (int) arg;
			switch (code) {
			case StateCode.REQUESTSTART:
				int f = JOptionPane.showConfirmDialog(this, "对方请求开始同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.SendMessage(StateCode.AGREESTART);
					EnableButton();
					gridPanel.getPrompt().setMessage("");
					o.addObserver(gridPanel);
					chessArray.setMy(false);
					setChessBox(false);
					chessArray.Clear();
				} else {
					SocketUtil.SendMessage(StateCode.DISAGREESTART);
				}
				break;
			case StateCode.AGREESTART:
				gridPanel.getPrompt().setMessage("");
				o.addObserver(gridPanel);
				chessArray.setMy(true);
				setChessBox(true);
				chessArray.Clear();
				break;
			case StateCode.DISAGREESTART:
				JOptionPane.showMessageDialog(this, "你被拒绝了！");
				gridPanel.getPrompt().setMessage("请点击开始");
				break;
			case StateCode.REQUESTREGRET:
				f = JOptionPane.showConfirmDialog(this, "对方请求悔棋，同意吗？", "提示信息", JOptionPane.YES_NO_OPTION);
				if (f == 0) {
					SocketUtil.SendMessage(StateCode.AGREEREGRET);
					chessArray.regret();
				} else {
					SocketUtil.SendMessage(StateCode.DISAGREEREGRET);
				}
				break;
			case StateCode.AGREEREGRET:
				chessArray.regret();
				break;
			case StateCode.CHANGECHESSBOX:
				if (chessArray.isMy())
					setChessBox(false);
				else
					setChessBox(true);
				break;
			case StateCode.LINKREST:
				chessArray.Clear();
				gridPanel.getPrompt().setPrompt("请点击开始", 48, Color.black, 1f);
				chessArray.setMy(false);
				break;
			}
		} else if (arg instanceof Color) {
			Color color = (Color) arg;
			if (color.getRGB() == SocketUtil.getColor().getRGB()) {
				gridPanel.getPrompt().setPrompt("你赢了", 70, Color.GREEN, 0.5f);
			} else {
				gridPanel.getPrompt().setPrompt("你输了", 70, Color.RED, 0.5f);
			}
			DisableButton();
		}
		repaint();
	}

	private void setChessBox(Boolean myself) {
		int i = 1, j = 0;
		ImageIcon black = new ImageIcon(getClass().getResource("../images/blackChess.png"));
		ImageIcon white = new ImageIcon(getClass().getResource("../images/whiteChess.png"));
		ImageIcon ii;
		if (myself) {
			i = 0;
			j = 1;
		}
		chessbox[i].setIcon(null);
		if (SocketUtil.getColor().getRGB() == Color.BLACK.getRGB()) {
			ii = black;
			if (j == 0)
				ii = white;
			chessbox[j].setIcon(ii);
		} else {
			ii = white;
			if (j == 0)
				ii = black;
			chessbox[j].setIcon(ii);
		}
	}

	@Override
	public void actionPerformed(ActionEvent a) {
		String cmd = a.getActionCommand();
		switch (cmd) {
		case "开始":
			SocketUtil.SendMessage(StateCode.REQUESTSTART);
			gridPanel.getPrompt().setPrompt("等待对方同意", 48, Color.black, 1f);
			gridPanel.repaint();
			break;
		case "悔棋":
			SocketUtil.SendMessage(StateCode.REQUESTREGRET);
			break;
		case "认输":
			break;
		case "回放":
			chessArray.replay(gridPanel);
			jb[s1.length - 1].setEnabled(false);
			break;
		}
	}

	public void DisableButton() {
		jb[0].setEnabled(true);
		jb[1].setEnabled(false);
		jb[2].setEnabled(false);
		jb[3].setEnabled(false);
	}

	public void EnableButton() {
		jb[0].setEnabled(false);
		jb[1].setEnabled(true);
		jb[2].setEnabled(true);
		jb[3].setEnabled(true);
	}

}
